package com.hckjsec.common.aspose;

import com.aspose.words.*;
import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.util.List;
import java.util.Map;

public class AsposeWord {

    public static boolean getLicense() {
        boolean result = false;
        try {
            Resource resource = new ClassPathResource("license.xml");
            InputStream license = resource.getInputStream();
            com.aspose.words.License aposeLic = new com.aspose.words.License();
            aposeLic.setLicense(license);
            result = true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * word转pdf
     * @param Address
     * @param path
     * @return
     */
    public static boolean word2pdf(String oldPath,String newPath) {
        boolean flag=false;
        try {
            if (!getLicense()) {          // 验证License 若不验证则转化出的PDP文档会有水印产生
                flag=false;
            }else{
                File file = new File(newPath);
                FileOutputStream os = new FileOutputStream(file);
                Document doc = new Document(oldPath);
//                doc.removeAllChildren();
//                doc.appendDocument(doc, ImportFormatMode.USE_DESTINATION_STYLES);//保留样式
                doc.acceptAllRevisions();
                doc.save(os, SaveFormat.PDF);
                os.close();
                flag=true;
            }
        }catch (Exception e) {
            flag=false;
            e.printStackTrace();
        }
        return flag;
    }



    //活体报告
    public static boolean generateReportVideo(String oldPath, String newPath, Map<String,String> DocHt, List<String> imgPathList) throws Exception {
        boolean flag=false;
        try {
            if (!getLicense()) {          // 验证License 若不验证则转化出的PDP文档会有水印产生
                flag=false;
            }else {
                File file = new File(newPath);
                FileOutputStream os = new FileOutputStream(file);
                Document doc = new Document(oldPath);
                DocumentBuilder builder = new DocumentBuilder(doc);
                int bookmarkcount = doc.getRange().getBookmarks().getCount();
                for (int markIndex = 0; markIndex < bookmarkcount; markIndex++) {
                    String markname = doc.getRange().getBookmarks().get(markIndex).getName();
                    if (DocHt.containsKey(markname)){
                        doc.getRange().getBookmarks().get(markIndex).setText(DocHt.get(markname));
                    }
                }
                if (imgPathList.size() == 0){
                    //现场照片-1
                    File oneFile = new File(DocHt.get("oneImg"));
                    if (oneFile.exists()) {
                        builder.moveToMergeField("oneImg");
                        Shape shape = builder.insertImage(fileConvertToByteArray(oneFile));
                        shape.setLeft(0);
                        shape.setTop(0);
                        shape.setWidth(120);
                        shape.setHeight(140);
                    }
                }else{
                    //现场照片-1
                    File oneFile = new File(imgPathList.get(0));
                    if(oneFile.exists()){
                        builder.moveToMergeField("oneImg");
                        Shape shape =builder.insertImage(fileConvertToByteArray(oneFile));
                        shape.setLeft(0);
                        shape.setTop(0);
                        shape.setWidth(120);
                        shape.setHeight(140);
                    }
                    //现场照片-2
                    File twoFile = new File(imgPathList.get(1));
                    if(twoFile.exists()){
                        builder.moveToMergeField("twoImg");
                        Shape shape =builder.insertImage(fileConvertToByteArray(twoFile));
                        shape.setLeft(0);
                        shape.setTop(0);
                        shape.setWidth(120);
                        shape.setHeight(140);
                    }
                    //现场照片-3
                    File threeFile = new File(imgPathList.get(2));
                    if(threeFile.exists()){
                        builder.moveToMergeField("threeImg");
                        Shape shape =builder.insertImage(fileConvertToByteArray(threeFile));
                        shape.setLeft(0);
                        shape.setTop(0);
                        shape.setWidth(120);
                        shape.setHeight(140);
                    }
                }
                doc.acceptAllRevisions();
                doc.save(os, SaveFormat.DOC);
                os.close();
                flag = true;
            }
        } catch (Exception e) {
            flag=false;
            e.printStackTrace();
        }
        return flag;
    }
    //身份核验
    public static boolean generateReportVerification(String oldPath, String newPath, Map<String,String> DocHt, List<String> imgPathList) throws Exception {
        boolean flag=false;
        try {
            if (!getLicense()) {          // 验证License 若不验证则转化出的PDP文档会有水印产生
                flag=false;
            }else {
                File file = new File(newPath);
                FileOutputStream os = new FileOutputStream(file);
                Document doc = new Document(oldPath);
                DocumentBuilder builder = new DocumentBuilder(doc);
                int bookmarkcount = doc.getRange().getBookmarks().getCount();
                for (int markIndex = 0; markIndex < bookmarkcount; markIndex++) {
                    String markname = doc.getRange().getBookmarks().get(markIndex).getName();
                    if (DocHt.containsKey(markname)){
                        doc.getRange().getBookmarks().get(markIndex).setText(DocHt.get(markname));
                    }
                }
                //正面
                File zmFile = new File(DocHt.get("zmImg"));
                if (zmFile.exists()) {
                    builder.moveToMergeField("zmImg");
                    Shape shape = builder.insertImage(fileConvertToByteArray(zmFile));
                    shape.setLeft(0);
                    shape.setTop(0);
                    shape.setWidth(225);
                    shape.setHeight(140);
                }

                //现场照片-1
                File oneFile = new File(imgPathList.get(0));
                if(oneFile.exists()){
                    builder.moveToMergeField("oneImg");
                    Shape shape =builder.insertImage(fileConvertToByteArray(oneFile));
                    shape.setLeft(0);
                    shape.setTop(0);
                    shape.setWidth(120);
                    shape.setHeight(140);
                }
                //现场照片-2
                File twoFile = new File(imgPathList.get(1));
                if(twoFile.exists()){
                    builder.moveToMergeField("twoImg");
                    Shape shape =builder.insertImage(fileConvertToByteArray(twoFile));
                    shape.setLeft(0);
                    shape.setTop(0);
                    shape.setWidth(120);
                    shape.setHeight(140);
                }
                //现场照片-3
                File threeFile = new File(imgPathList.get(2));
                if(threeFile.exists()){
                    builder.moveToMergeField("threeImg");
                    Shape shape =builder.insertImage(fileConvertToByteArray(threeFile));
                    shape.setLeft(0);
                    shape.setTop(0);
                    shape.setWidth(120);
                    shape.setHeight(140);
                }
                doc.acceptAllRevisions();
                doc.save(os, SaveFormat.DOC);
                os.close();
                flag = true;
            }
        } catch (Exception e) {
            flag=false;
            e.printStackTrace();
        }
        return flag;
    }

    //身份核验
    public static boolean generateSdkReportVerification(String oldPath, String newPath, Map<String,String> DocHt) throws Exception {
        boolean flag=false;
        try {
            if (!getLicense()) {          // 验证License 若不验证则转化出的PDP文档会有水印产生
                flag=false;
            }else {
                File file = new File(newPath);
                FileOutputStream os = new FileOutputStream(file);
                Document doc = new Document(oldPath);
                DocumentBuilder builder = new DocumentBuilder(doc);
                int bookmarkcount = doc.getRange().getBookmarks().getCount();
                for (int markIndex = 0; markIndex < bookmarkcount; markIndex++) {
                    String markname = doc.getRange().getBookmarks().get(markIndex).getName();
                    if (DocHt.containsKey(markname)){
                        doc.getRange().getBookmarks().get(markIndex).setText(DocHt.get(markname));
                    }
                }
                //现场照片-1
                File zmFile = new File(DocHt.get("oneImg"));
                if (zmFile.exists()) {
                    builder.moveToMergeField("oneImg");
                    Shape shape = builder.insertImage(fileConvertToByteArray(zmFile));
                    shape.setLeft(0);
                    shape.setTop(0);
                    shape.setWidth(195);
                    shape.setHeight(260);
                }
                doc.acceptAllRevisions();
                doc.save(os, SaveFormat.DOC);
                os.close();
                flag = true;
            }
        } catch (Exception e) {
            flag=false;
            e.printStackTrace();
        }
        return flag;
    }





    //活体报告转成png
    public static boolean generateReportToPng(String oldPath,String newPath) throws Exception {
        boolean flag=false;
        try {
            if (!getLicense()) {          // 验证License 若不验证则转化出的PDP文档会有水印产生
                flag=false;
            }else {
                File file = new File(newPath);
                FileOutputStream os = new FileOutputStream(file);
                Document doc = new Document(oldPath);
                DocumentBuilder builder = new DocumentBuilder(doc);
                doc.acceptAllRevisions();
                doc.save(os, SaveFormat.PNG);
                os.close();
                flag = true;
            }
        } catch (Exception e) {
            flag=false;
            e.printStackTrace();
        }
        return flag;
    }

    private static byte[] fileConvertToByteArray(File file) {
        byte[] data = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            data = baos.toByteArray();
            fis.close();
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }





    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        word2pdf("D:/11/委托笔录.doc","D:/11/委托笔录.pdf");
        long end = System.currentTimeMillis();
        System.out.println("转换完成..用时：" + (end - start) + "ms.");
    }
}
